package com.zbs.framework.dao.security.service.impl;


import com.zbs.framework.dao.function.repository.FunctionRepository;
import com.zbs.framework.dao.permission.service.PermissionService;
import com.zbs.framework.dao.rolefunction.service.UserFunctionService;
import com.zbs.framework.dao.rolefunction.service.model.UserFunctionInfo;
import com.zbs.framework.dao.security.service.SecurityPermissionService;
import com.zbs.framework.dao.user.repository.UserRoleRepository;
import com.zbs.framework.security.config.FrameworkConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Zbs
 * @since 2021/5/26 0026 10:18
 */
@Transactional
@Service
public class SecurityPermissionServiceImpl implements SecurityPermissionService {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private UserFunctionService userFunctionService;
    @Autowired
    private FunctionRepository functionRepository;
    @Autowired
    private UserRoleRepository userRoleRepository;

    @Override
    public List<String> getUserAllPermissionByUserId(Long userId) {
        if (userId == null){
            return null;
        }
        final List<Long> adminIdList = Arrays.asList(FrameworkConstants.DEFAULT_ADMIN);
        if (FrameworkConstants.DO_OPEN_PERMISSIONS && !adminIdList.contains(userId)){
            //查询用户所拥有的权限
            Integer[] roleIds = this.userRoleRepository.findRoleIds(userId);
            return new ArrayList<>(this.userFunctionService.getAllUserPermissions(userId, roleIds));
        }else {
            //关闭权限 或者 管理员 查询所有权限
            return this.permissionService.findAllPerms().stream().map(permission -> String.format("%s::%s",permission.getServiceName(),permission.getName())).collect(Collectors.toList());

        }
    }

    @Override
    public List<UserFunctionInfo> getUserAllFunctionByUserId(Long userId) {
        if (userId == null){
            return null;
        }
        final List<Long> adminIdList = Arrays.asList(FrameworkConstants.DEFAULT_ADMIN);
        List<UserFunctionInfo> functions;
        if (adminIdList.contains(userId)){
            //管理员查询所有功能
            functions = this.functionRepository.findAll().stream().map(UserFunctionInfo::new).collect(Collectors.toList());
        }else {
            //查询用户所拥有的权限
            functions = this.userFunctionService.getUserAllFunctions(userId);
        }
        return functions;
    }

    @Override
    public boolean existsUserAllFunctions(Long userId, String identifier) {
        if (userId == null){
            return false;
        }
        final List<Long> adminIdList = Arrays.asList(FrameworkConstants.DEFAULT_ADMIN);
        if (adminIdList.contains(userId)){
            //管理员查询所有功能
            return true;
        }else {
            //查询用户所拥有的权限
            return this.userFunctionService.existsUserAllFunctions(userId,identifier);
        }
    }
}